package ihm.manes_ho.agenda.dialog;

import ihm.manes_ho.agenda.R;
import ihm.manes_ho.agenda.vo.ArticleVO;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

/**
 * ArticleDialog is showed when users perform a "add/update an article"
 */
public class ArticleDialog extends Dialog {
	private EditText txtName;
	private EditText txtQuantity;
	private EditText txtPrice;
	private Button btnCancel;
	private Button btnOk;
	private OnOkClickListener onOkListener;
	private Spinner spChooseArticle;
	private int type;

	/**
	 * ArticleDialog - my first dialog..
	 * @param context The context the dialog is to run in.
	 * @param type type of either action add or edit. type = 0 if action is 'add', type = 1 if action is 'edit'
	 * @param callBack How the parent is notified that the Ok button is clicked.
	 */
	public ArticleDialog(Context context, int type, OnOkClickListener callBack) {
		super(context);
		this.onOkListener = callBack;
		this.type = type;

		//initialize dialog's components
		initComponents();
	}

	private void initComponents() {
		setContentView(R.layout.dialog_article);
		setTitle(type == 0 ? R.string.add_article_title : R.string.edit_article_title);
		//setOwnerActivity(this);

		initEditTexts();

		initCancelButton();

		initOkButton();

		initArticleSpinner();
	}
	
	private void initEditTexts() {
		//name
		txtName = (EditText) this.findViewById(R.id.text_edit_name);		

		//quantity
		txtQuantity = (EditText) this.findViewById(R.id.text_edit_quantity);		

		//price
		txtPrice = (EditText) this.findViewById(R.id.text_edit_price);		
	}

	private void initCancelButton() {
		//cancel button
		btnCancel = (Button) this.findViewById(R.id.button_cancel);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ArticleDialog.this.dismiss(); //dismiss();
				ArticleDialog.this.clear(); //clear();
			}
		});
	}

	private void initOkButton() {
		//ok button
		btnOk = (Button) this.findViewById(R.id.button_ok);
		btnOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onOkListener != null) {

					//calls the callback method
					onOkListener.onOkClick(
							ArticleDialog.this.type,
							txtName.getText().toString(), 
							Integer.valueOf(txtQuantity.getText().toString()),
							Double.valueOf(txtPrice.getText().toString())
					);
					ArticleDialog.this.dismiss(); //dismiss();
					ArticleDialog.this.clear(); //clear();
				}
			}
		});
	}

	private void initArticleSpinner() {
		spChooseArticle = (Spinner) this.findViewById(R.id.spinner_articles);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this.getContext(), R.array.articles, android.R.layout.simple_spinner_item);

		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spChooseArticle.setAdapter(adapter);

		spChooseArticle.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			boolean firsttimeSelected = true;
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				//normally, after the spinner is completely loaded, its first item will be selected automatically
				//we'll do nothing for this time..
				if (firsttimeSelected) {
					firsttimeSelected = false;
					return;
				}
				txtName.setText(arg0.getItemAtPosition(position).toString());
				Log.v("spinner>onItemSelected", arg0.getItemAtPosition(position).toString());

			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Log.v("spinner>onNothingSelected", "nothing...");        		
			}
		});       
	}
	
	/**
	 * This method is used to clear the article dialog's text content
	 */
	private void clear() {
		txtName.setText("");
		txtQuantity.setText("0");
		txtPrice.setText("0");
	}
	
	/**
	 * set data for dialog..
	 * @param article
	 */
	public void setData(ArticleVO article) {
		txtName.setText(article.getName());
		txtQuantity.setText(String.valueOf(article.getQuantity()));
		txtPrice.setText(String.valueOf(article.getPrice()));
		Log.v("setData", article.toString());
	}
	
	/**
	 * set article's name
	 */
	public void setName(CharSequence msg) {
		txtName.setText(msg);
	}

	/**
	 * set number of articles
	 */
	public void setNumber(Integer number) {
		txtName.setText(String.valueOf(number));
	}

	/**
	 * set price of article
	 */
	public void setPrice(Double price) {
		txtPrice.setText(String.valueOf(price));
	}

	/**
	 * OnOkClickListener
	 */
	public static interface OnOkClickListener {

		/**
		 * callBack method - this method will be called when the Ok button is clicked
		 * @param type type of either action add or edit. type = 0 if action is 'add', type = 1 if action is 'edit'
		 * @param name article name
		 * @param number article number
		 * @param price article price
		 */
		void onOkClick(int type, String name, Integer number, Double price);
	}
}
